#include "R69434.h"

void vcomSetting(void)
{
    sendFormat(GEN, 8);
    W_D(0xD5);
    W_D(0x06);
    W_D(0x00);
    W_D(0x00);
    W_D(0x01);
    W_D(0x18);
    W_D(0x01);
    W_D(0x18);
}

void checkPowerState(void)
{
    U16 ids[26];
    U8 mipiAddr;
    U8 mipiReadLen;
    U8 i;

    //DebugPrintf("Enter initial\r\n");

    //NT35523MTP_CHECK();
    //SPI_READ_ID();

    sendFormat(DCS, 1);
    W_D(0x11);
    delay_ms(100);
    sendFormat(DCS, 1);
    W_D(0x29);
    delay_ms(100);

    sendFormat(DCS, 2);
    W_D(0x11);
    W_D(0x00);
    delay_ms(100);
    sendFormat(DCS, 2);
    W_D(0x29);
    W_D(0x00);
    delay_ms(100);

    //read lcm id
    //mipiAddr = 0xDA;
    //mipiAddr = 0x0c; // get_pixel_format  default: 0x77
    //mipiAddr = 0x26; //set_gamma_curve 0x01
    //mipiReadLen = 1;

    //mipiAddr = 0x04; //
    //mipiReadLen = 13;

    mipiAddr = 0x0A; //get_power_mode
    mipiReadLen = 1;

    /*readMipi(3, mipiReadLen, mipiAddr, ids);
    for(i =0; i < mipiReadLen; i++) {
    	DebugPrintf("mipi read 0x%x = 0x%x \r\n",mipiAddr, ids[i]);
	}
	*/

    readMipi(DCS, 3, mipiReadLen, mipiAddr, ids);
    for (i = 0; i < mipiReadLen; i++)
    {
        printf("mipi read 0x%x = 0x%x \r\n", mipiAddr, ids[i]);
        //UART1_SendData8(ids[i] >> 8);
        //UART1_SendData8(ids[i]);
    }
}

void R69434_init(void)
{
    printf("R69434 init\r\n");

    checkPowerState();

    sendFormat(GEN, 2);
    W_D(0xB0);
    W_D(0x04);

    sendFormat(GEN, 2);
    W_D(0xD6);
    W_D(0x01);

    sendFormat(DCS, 2);
    W_D(0x51);
    W_D(0xFF);

    sendFormat(DCS, 2);
    W_D(0x53);
    W_D(0x2C);

    sendFormat(DCS, 2);
    W_D(0x55);
    W_D(0x00);

    sendFormat(GEN, 11);
    W_D(0xE1);
    W_D(0x03);
    W_D(0x00);
    W_D(0x00);
    W_D(0xFF);
    W_D(0xFF);
    W_D(0xFF);
    W_D(0xFF);
    W_D(0xFF);
    W_D(0xFF);
    W_D(0x7F);
    delay_ms(100);

    sendFormat(GEN, 7);
    W_D(0xE2);
    W_D(0x01);
    W_D(0x00);
    W_D(0x00);
    W_D(0xFF);
    W_D(0xFF);
    W_D(0xFF);

    sendFormat(GEN, 7);
    W_D(0xB3);
    W_D(0x14);
    W_D(0x08);
    W_D(0x00);
    W_D(0x00);
    W_D(0x00);
    W_D(0x00);

    sendFormat(GEN, 2);
    W_D(0xB4);
    W_D(0x0C);

    sendFormat(GEN, 3);
    W_D(0xB6);
    W_D(0x3A);
    W_D(0xD3);

    sendFormat(GEN, 2);
    W_D(0xB7);
    W_D(0x00);

    sendFormat(GEN, 7);
    W_D(0xB8);
    W_D(0x07);
    W_D(0x87);
    W_D(0x26);
    W_D(0x18);
    W_D(0x00);
    W_D(0x32);

    sendFormat(GEN, 7);
    W_D(0xB9);
    W_D(0x07);
    W_D(0x75);
    W_D(0x61);
    W_D(0x20);
    W_D(0x16);
    W_D(0x87);

    sendFormat(GEN, 7);
    W_D(0xBA);
    W_D(0x07);
    W_D(0x70);
    W_D(0x81);
    W_D(0x20);
    W_D(0x45);
    W_D(0xB4);

    sendFormat(GEN, 2);
    W_D(0xC0);
    W_D(0xFF);

    /*sendFormat(DCS, 1);
    W_D(0x10);
    delay_ms(100);
    
    
    sendFormat(DCS, 2);
    W_D(0x10);
    W_D(0x00);
    delay_ms(100);*/

    //DO NOT COMMENT
    sendFormat(GEN, 37);
    W_D(0xC1);
    W_D(0x04);
    W_D(0x61);
    W_D(0x00);
    W_D(0x20);
    W_D(0x8C);
    W_D(0xA4);
    W_D(0xD6);
    W_D(0xFF);
    W_D(0xFF);
    W_D(0xFF);
    W_D(0xFF);
    W_D(0x7F);
    W_D(0x73);
    W_D(0xEF);
    W_D(0xB9);
    W_D(0xF6);
    W_D(0xFF);
    W_D(0xFF);
    W_D(0xFF);
    W_D(0xFF);
    W_D(0xBF);
    W_D(0x54);
    W_D(0x22);
    W_D(0x02);
    W_D(0x00);
    W_D(0x00);
    W_D(0x00);
    W_D(0x00);
    W_D(0x00);
    W_D(0x00);
    W_D(0x62);
    W_D(0x03);
    W_D(0x00);
    W_D(0x22);
    W_D(0x00);
    W_D(0x01);

    sendFormat(GEN, 10);
    W_D(0xC2);
    W_D(0x31);
    W_D(0xF7);
    W_D(0x80);
    W_D(0x00);
    W_D(0x09);
    W_D(0x00);
    W_D(0x04);
    W_D(0x00);
    W_D(0x00);

    sendFormat(GEN, 4);
    W_D(0xC3);
    W_D(0x00);
    W_D(0x00);
    W_D(0x00);

    sendFormat(GEN, 13);
    W_D(0xC4);
    W_D(0x70);
    W_D(0x00);
    W_D(0x00);
    W_D(0x00);
    W_D(0x00);
    W_D(0x00);
    W_D(0x00);
    W_D(0x00);
    W_D(0x00);
    W_D(0x05);
    W_D(0x04);
    W_D(0x00);
    //DO NOT COMMENT
    sendFormat(GEN, 21);
    W_D(0xC6);
    W_D(0x78);
    W_D(0x73);
    W_D(0x75);
    W_D(0x0A);
    W_D(0x14);
    W_D(0x0B);
    W_D(0x16);
    W_D(0x00);
    W_D(0x00);
    W_D(0x00);
    W_D(0x00);
    W_D(0x00);
    W_D(0x00);
    W_D(0x00);
    W_D(0x00);
    W_D(0x00);
    W_D(0x00);
    W_D(0x1C);
    W_D(0x17);
    W_D(0x09);

    sendFormat(GEN, 31);
    W_D(0xC7);
    W_D(0x06);
    W_D(0x0D);
    W_D(0x16);
    W_D(0x20);
    W_D(0x2F);
    W_D(0x3E);
    W_D(0x48);
    W_D(0x58);
    W_D(0x3C);
    W_D(0x44);
    W_D(0x4F);
    W_D(0x5C);
    W_D(0x65);
    W_D(0x6D);
    W_D(0x76);
    W_D(0x06);
    W_D(0x0D);
    W_D(0x16);
    W_D(0x20);
    W_D(0x2F);
    W_D(0x3E);
    W_D(0x48);
    W_D(0x58);
    W_D(0x3C);
    W_D(0x44);
    W_D(0x4F);
    W_D(0x5C);
    W_D(0x65);
    W_D(0x6D);
    W_D(0x76);

    sendFormat(GEN, 20);
    W_D(0xC8);
    W_D(0x01);
    W_D(0x00);
    W_D(0x00);
    W_D(0x00);
    W_D(0x00);
    W_D(0xB4);
    W_D(0x00);
    W_D(0x00);
    W_D(0x00);
    W_D(0x00);
    W_D(0x00);
    W_D(0xB4);
    W_D(0x00);
    W_D(0x00);
    W_D(0x00);
    W_D(0x00);
    W_D(0x00);
    W_D(0xFC);
    W_D(0x00);

    sendFormat(GEN, 33);
    W_D(0xCA);
    W_D(0x00);
    W_D(0x80);
    W_D(0x80);
    W_D(0x80);
    W_D(0x80);
    W_D(0x80);
    W_D(0x80);
    W_D(0x80);
    W_D(0x08);
    W_D(0x20);
    W_D(0x80);
    W_D(0x80);
    W_D(0x0A);
    W_D(0x4A);
    W_D(0x37);
    W_D(0xA0);
    W_D(0x55);
    W_D(0xF8);
    W_D(0x0C);
    W_D(0x0C);
    W_D(0x20);
    W_D(0x10);
    W_D(0x3F);
    W_D(0x3F);
    W_D(0x00);
    W_D(0x00);
    W_D(0x10);
    W_D(0x10);
    W_D(0x3F);
    W_D(0x3F);
    W_D(0x3F);
    W_D(0x3F);
    //DO NOT COMMENT
    sendFormat(GEN, 13);
    W_D(0xCB);
    W_D(0x3F);
    W_D(0xC0);
    W_D(0x0F);
    W_D(0xF0);
    W_D(0x03);
    W_D(0x00);
    W_D(0x03);
    W_D(0x00);
    W_D(0x03);
    W_D(0x00);
    W_D(0x00);
    W_D(0xC0);

    //DO NOT COMMENT
    sendFormat(GEN, 2);
    W_D(0xCC);
    W_D(0x11);

    sendFormat(GEN, 24);
    W_D(0xCE);
    W_D(0x7D);
    W_D(0x40);
    W_D(0x48);
    W_D(0x56);
    W_D(0x67);
    W_D(0x78);
    W_D(0x88);
    W_D(0x98);
    W_D(0xA7);
    W_D(0xB5);
    W_D(0xC3);
    W_D(0xD1);
    W_D(0xDE);
    W_D(0xE9);
    W_D(0xF2);
    W_D(0xFA);
    W_D(0xFF);
    W_D(0x04);
    W_D(0x00);
    W_D(0x0F);
    W_D(0x0F);
    W_D(0x42);
    W_D(0x10);

    sendFormat(GEN, 6);
    W_D(0xD0);
    W_D(0x44);
    W_D(0x81);
    W_D(0xBB);
    W_D(0x15);
    W_D(0x94);

    sendFormat(GEN, 26);
    W_D(0xD3);
    W_D(0x0B);
    W_D(0x33);
    W_D(0xBF);
    W_D(0xBB);
    W_D(0xB3);
    W_D(0x33);
    W_D(0x33);
    W_D(0x37);
    W_D(0x00);
    W_D(0x01);
    W_D(0x00);
    W_D(0xA0);
    W_D(0x98);
    W_D(0xA0);
    W_D(0x00);
    W_D(0x39);
    W_D(0x39);
    W_D(0x33);
    W_D(0x3B);
    W_D(0x37);
    W_D(0x72);
    W_D(0x07);
    W_D(0x3D);
    W_D(0xBF);
    W_D(0x99);

    vcomSetting();

    /*sendFormat(DCS, 1);
    W_D(0x11);
    delay_ms(100);
    sendFormat(DCS, 1);
    W_D(0x29);
    delay_ms(100);*/

    checkPowerState();
}